package com.guigu.pd.auth.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.guigu.pd.auth.utils.BaiduMapUtils;
import org.springframework.web.bind.annotation.*;

import com.guigu.pd.auth.entity.CoreOrgEntity;
import com.guigu.pd.auth.service.CoreOrgService;
import com.guigu.common.utils.PageUtils;
import com.guigu.common.utils.R;

import javax.annotation.Resource;


/**
 * 组织
 *
 * @author deyran
 * @email deyran@gmail.com
 * @date 2022-07-19 19:05:57
 */
@RestController
@RequestMapping("auth/coreorg")
public class CoreOrgController {
    @Resource
    private CoreOrgService coreOrgService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = coreOrgService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 列表
     */
    @RequestMapping("/list/tree")
    public R tree(@RequestParam Map<String, Object> params){
        List<CoreOrgEntity> page = coreOrgService.listWithTree(params);

        return R.ok().put("page", page);
    }

    //根据网点id获取pid
    @GetMapping("/getpidbyid/{id}")
    public Long getpidbyid(@PathVariable("id") String id){

        CoreOrgEntity byId = coreOrgService.getById(id);
        return byId.getParentId();

    }

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("auth:coreorg:info")
    public R info(@PathVariable("id") String id){

        Long iid = Long.valueOf(id);
        LambdaQueryWrapper<CoreOrgEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(CoreOrgEntity::getId,iid);
		CoreOrgEntity coreOrg = coreOrgService.getOne(lambdaQueryWrapper);

        return R.ok().put("coreOrg", coreOrg);
    }

    @GetMapping("/getcoreorgbyId/{id}")
    public String getcoreorgbyId(@PathVariable("id")String id){
        CoreOrgEntity coreOrg = coreOrgService.getById(id);

        return coreOrg.getName();
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("auth:coreorg:save")
    public R save(@RequestBody CoreOrgEntity coreOrg){
		coreOrgService.save(coreOrg);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("auth:coreorg:update")
    public R update(@RequestBody CoreOrgEntity coreOrg){
		coreOrgService.updateById(coreOrg);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody String[] checkedKeys){
		coreOrgService.removeByIds(Arrays.asList(checkedKeys));

        return R.ok();
    }

    /**
     * 根据id查询组织名称
     * @param id
     * @return
     */
    @GetMapping("/getcornamebyid/{id}")
    public String getcornamebyid(@PathVariable("id") String id){
        CoreOrgEntity coreOrg = coreOrgService.getById(id);
        if (coreOrg!=null){
            return coreOrg.getName();
        }else {
            return null;
        }

    }

    //根据id 修改作业范围 (保存机构作业范围)
    @GetMapping("/savejgfw/{jgjd}/{jgwd}/{jgbj}/{jgbh}")
    public R savejgfw(@PathVariable("jgjd") String jgjd,
                           @PathVariable("jgwd") String jgwd,
                           @PathVariable("jgbj") String jgbj,
                           @PathVariable("jgbh") String jgbh){

        LambdaUpdateWrapper<CoreOrgEntity> lambdaUpdateWrapper = new LambdaUpdateWrapper<CoreOrgEntity>();
        lambdaUpdateWrapper.eq(CoreOrgEntity::getId,jgbh).set(CoreOrgEntity::getLatitude,jgjd)
                .set(CoreOrgEntity::getLongitude,jgwd)
                .set(CoreOrgEntity::getCoreOrgRadius,jgbj);

        boolean update = coreOrgService.update(lambdaUpdateWrapper);

        if(update){
            return  R.ok("添加成功");
        }
        return  R.ok("添加失败");
    }

    //根据区县id 和 机构级别4 找到符合要求的机构id
    @GetMapping("/chazhaofhid/{dljwd}/{qxid}")
    public String chazhaofhid(@PathVariable("dljwd") String dljwd,@PathVariable("qxid") String qxid){

        String jgid = "";

        int jl = 0;

        LambdaQueryWrapper<CoreOrgEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(CoreOrgEntity::getCountyId,qxid).eq(CoreOrgEntity::getOrgType,4);
        List<CoreOrgEntity> list = coreOrgService.list(lambdaQueryWrapper);

        if(list.size()==0){
            return "qqq";
        }
        //循环判断发件人的发件坐标与网点坐标距离是否在网点作业范围半径内
        for (CoreOrgEntity coreOrgEntity : list) {

            jl = coreOrgEntity.getCoreOrgRadius();
            //获取机构的经度
            String longitude = coreOrgEntity.getLongitude();
            //获取机构的纬度
            String latitude = coreOrgEntity.getLatitude();
            //计算订单距离和网点位置距离
            double distance =BaiduMapUtils.getDistance(dljwd, longitude + "," + latitude);

            //获取到最近的网点id
            if(distance<jl){
                jgid = String.valueOf(coreOrgEntity.getId());
            }
        }

         if(jgid.equals("")){
             jgid = String.valueOf(list.get(0).getId());
         }

        return jgid;
    }

    @RequestMapping("/getagency")
    public R getAgency(){
        List<CoreOrgEntity> list = coreOrgService.list();

        return R.ok().put("agency", list);
    }



}
